package cn.iocoder.yudao.module.wine.controller.admin.complaininfo;

import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaintype.ComplainTypeDO;
import cn.iocoder.yudao.module.wine.dal.mysql.businessuser.BusinessUserMapper;
import cn.iocoder.yudao.module.wine.service.complaintype.ComplainTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.USER_NOT_EXISTS;

import cn.iocoder.yudao.module.wine.controller.admin.complaininfo.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaininfo.ComplainInfoDO;
import cn.iocoder.yudao.module.wine.service.complaininfo.ComplainInfoService;

@Tag(name = "管理后台 - 投诉信息")
@RestController
@RequestMapping("/wine/complain-info")
@Validated
public class ComplainInfoController {

    @Resource
    private ComplainInfoService complainInfoService;

    @Resource
    private ComplainTypeService complainTypeService;

    @Resource
    private BusinessUserMapper businessUserMapper;
    @Resource
    private MemberUserService memberUserService;


    @PostMapping("/create")
    @Operation(summary = "创建投诉信息")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:create')")
    public CommonResult<Long> createComplainInfo(@Valid @RequestBody ComplainInfoSaveReqVO createReqVO) {
        return success(complainInfoService.createComplainInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉信息")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:update')")
    public CommonResult<Boolean> updateComplainInfo(@Valid @RequestBody ComplainInfoSaveReqVO updateReqVO) {
        complainInfoService.updateComplainInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投诉信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:complain-info:delete')")
    public CommonResult<Boolean> deleteComplainInfo(@RequestParam("id") Long id) {
        complainInfoService.deleteComplainInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:query')")
    public CommonResult<ComplainInfoRespVO> getComplainInfo(@RequestParam("id") Long id) {
        ComplainInfoDO complainInfo = complainInfoService.getComplainInfo(id);
        ComplainInfoRespVO complainInfoRespVO = BeanUtils.toBean(complainInfo, ComplainInfoRespVO.class);

        long userId = complainInfo.getUserId();
        //获取用户信息
        MemberUserDO user = memberUserService.getUser(userId);
        if(user != null) {
            complainInfoRespVO.setName(user.getName());
            complainInfoRespVO.setNickname(user.getNickname());
            complainInfoRespVO.setUserMobile(user.getMobile());
        }
        //获取商家信息
        BusinessUserDO businessUserDO = businessUserMapper.selectOne(new LambdaQueryWrapper<BusinessUserDO>().eq(BusinessUserDO::getUserId, userId));
        if(businessUserDO != null) {
            complainInfoRespVO.setUserCompanyName(businessUserDO.getCompanyName());
            complainInfoRespVO.setUserCompanyAddress(businessUserDO.getCompanyAddress());
            String format = AreaUtils.format(businessUserDO.getAreaId()," ");
            complainInfoRespVO.setUserAreaName(format);
        }
        //投诉类型信息
        ComplainTypeDO complainTypeDO = complainTypeService.getComplainType(complainInfo.getTypeId());
        if(complainTypeDO != null) {
            complainInfoRespVO.setTypeName(complainTypeDO.getTypeName());
        }
        return success(complainInfoRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得投诉信息分页")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:query')")
    public CommonResult<PageResult<ComplainInfoRespVO>> getComplainInfoPage(@Valid ComplainInfoPageReqVO pageReqVO) {
        PageResult<ComplainInfoRespVO> pageResult = complainInfoService.getComplainInfoPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/typeList")
    @Operation(summary = "获得投诉分类信息")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:query')")
    public CommonResult<List<ComplainTypeDO>> getComplainTypeList() {
        List<ComplainTypeDO> list = complainTypeService.selectTypeList();
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投诉信息 Excel")
    @PreAuthorize("@ss.hasPermission('wine:complain-info:export')")
    @OperateLog(type = EXPORT)
    public void exportComplainInfoExcel(@Valid ComplainInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ComplainInfoRespVO> list = complainInfoService.getComplainInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投诉信息.xls", "数据", ComplainInfoRespVO.class,
                        BeanUtils.toBean(list, ComplainInfoRespVO.class));
    }

}
